package api

import (
	"api-gateway/basic/config"
	__ "api-gateway/basic/proto"
	"api-gateway/handler/request"
	"api-gateway/pkg"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"path/filepath"
)

func SendSms(c *gin.Context) {
	var param request.SendSms
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "参数错误",
		})
	}
	_, err := config.UserCline.SendSms(c, &__.SendSmsReq{
		Phone: param.Phone,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"mes":  "验证码发送成功",
	})
	return
}

func Register(c *gin.Context) {
	var param request.Register
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "参数错误",
		})
	}
	r, err := config.UserCline.GetPhoneByPhone(c, &__.GetPhoneByPhoneReq{
		Phone: param.Phone,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  err.Error() + err.Error(),
		})
		return
	}
	if r.UserId != 0 {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "该用户已注册",
		})
		return
	}
	create, err := config.UserCline.CreateUser(c, &__.CreateUserReq{
		Phone:    param.Phone,
		Password: param.Password,
		Code:     param.Code,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"mes":  "注册成功",
		"data": create.UserId,
	})
	return
}

func Login(c *gin.Context) {
	var param request.Login
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "参数错误",
		})
	}
	r, err := config.UserCline.GetPhoneByPhone(c, &__.GetPhoneByPhoneReq{
		Phone: param.Phone,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  err.Error(),
		})
		return
	}
	if r.UserId == 0 {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "该用户未注册",
		})
		return
	}
	if pkg.Md5(r.Password) != param.Password {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "密码错误",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"mes":  "登陆成功",
		"data": r.UserId,
	})
	return
}

func Upload(c *gin.Context) {
	file, err := c.FormFile("file")
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "参数错误",
		})
		return
	}

	ext := filepath.Ext(file.Filename)
	if ext != ".mp4" {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "视频类型不一致",
		})
		return
	}
	if file.Size > 100*1024*1024 {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "视频大小大于100mb",
		})
		return
	}
	dsn := "D:\\gowork\\src\\haw\\api-gateway\\static\\upload\\" + file.Filename
	c.SaveUploadedFile(file, dsn)
	pkg.Oss(file.Filename, dsn)
	c.JSON(http.StatusBadRequest, gin.H{
		"code": 400,
		"mes":  fmt.Sprintf("https://wangziyue123.oss-cn-shanghai.aliyuncs.com/") + file.Filename,
	})
}

func VideoAdd(c *gin.Context) {
	var param request.VideoAdd
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "参数错误",
		})
	}
	r, err := config.UserCline.CreateVideo(c, &__.CreateVideoReq{
		UserId:       param.UserId,
		Title:        param.Title,
		Desc:         param.Dsc,
		UploadUrl:    param.UploadUrl,
		LikeCount:    param.LikeCount,
		CollectCount: param.CollectCount,
		CommentCount: param.CommentCount,
		Hot:          param.Hot,
		PayCount:     param.PayCount,
		IsVip:        param.IsVip,
		TagId:        param.TagId,
		TypeId:       param.TypeId,
		Status:       param.Status,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"mes":  "视频信息录入成功",
		"data": r.VideoId,
	})
	return
}

func VideoCheck(c *gin.Context) {
	var param request.VideoCheck
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "参数错误",
		})
	}
	r, err := config.UserCline.VideoCheck(c, &__.VideoCheckReq{
		Id:     param.Id,
		Status: param.Status,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"mes":  "审核视频信息成功",
		"data": r.VideoId,
	})
	return
}

func CreateType(c *gin.Context) {
	var param request.TypeAdd
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "参数错误",
		})
	}
	r, err := config.UserCline.CreateType(c, &__.CreateTypeReq{
		TypeName: param.TypeName,
		PId:      param.Pid,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"mes":  "类型构建成功",
		"data": r.TypeId,
	})
	return
}

func CreateTag(c *gin.Context) {
	var param request.TagAdd
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "参数错误",
		})
	}
	r, err := config.UserCline.CreateTag(c, &__.CreateTagReq{
		TagName: param.TagName,
		TagType: param.TagType,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"mes":  "类型构建成功",
		"data": r.TagId,
	})
	return
}

func CountVideoType(c *gin.Context) {
	var param request.VideoType
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "参数错误",
		})
	}
	r, err := config.UserCline.CountVideoType(c, &__.CountVideoTypeReq{
		Keyword:      param.Keyword,
		TypeName:     param.TypeName,
		Sort:         param.Sort,
		PayCount:     param.PayCount,
		Status:       param.Status,
		MemberStatus: param.MemberStatus,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"mes":  "视频展示成功",
		"data": r,
	})
	return
}

func CreateDram(c *gin.Context) {
	var param request.CreateDram
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "参数错误",
		})
	}
	r, err := config.UserCline.CreateDram(c, &__.CreateDramReq{
		UserId:    param.UserId,
		Name:      param.Name,
		VideoId:   param.VideoId,
		Dsc:       param.Dsc,
		DramCount: param.DramCount,
		DramTime:  param.DramTime,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"mes":  "剧集添加成功",
		"data": r,
	})
	return
}

func AppendAdd(c *gin.Context) {
	var param request.AppendAdd
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "参数错误",
		})
	}
	r, err := config.UserCline.AppendAdd(c, &__.AppendAddReq{
		UserId: param.UserId,
		DramId: param.DramId,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"mes":  "追更成功",
		"data": r.DramId,
	})
	return
}

func CollectAdd(c *gin.Context) {
	var param request.CollectAdd
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "参数错误",
		})
	}
	r, err := config.UserCline.CollectAdd(c, &__.CollectAddReq{
		UserId:   param.UserId,
		DramId:   param.DramId,
		DramTime: param.DramTime,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"mes":  "收藏成功",
		"data": r.CollectId,
	})
	return
}

func RecordList(c *gin.Context) {
	var param request.RecordList
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "参数错误",
		})
	}
	r, err := config.UserCline.RecordList(c, &__.RecordListReq{
		Keyword: param.KeyWord,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"mes":  "历史展示成功",
		"data": r,
	})
	return
}

func MemberOrder(c *gin.Context) {
	var param request.MemberOrder
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "参数错误",
		})
	}
	r, err := config.UserCline.MemberOrder(c, &__.MemberOrderReq{
		UserId: param.UserId,
		DramId: param.DramId,
		Price:  param.Price,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"mes":  "会员订购成功",
		"data": r,
	})
	return
}

func MemberShow(c *gin.Context) {
	var param request.MemberShow
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "参数错误",
		})
	}
	r, err := config.UserCline.MemberShow(c, &__.MemberShowReq{
		UserId: param.UserId,
		DramId: param.DramId,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"mes":  "会员审核成功",
		"data": r,
	})
	return
}

func VideoHot(c *gin.Context) {
	var param request.VideoHot
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "参数错误",
		})
	}
	r, err := config.UserCline.VideoHotList(c, &__.VideoHotReq{
		Sort: param.Sort,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"mes":  "热度排行榜展示成功",
		"data": r,
	})

}
